<?php
defined('IN_TS') or die('Access Denied.');

/* 
 * 淘宝客分类管理
 */

switch($ts) {
	//分类列表
	case "list":
		$arrItems = $new['tbshare']->findAll('tbshare',null,"tbshareid DESC");
		$tmp = $db->fetch_all_assoc("select cateid,catename from ".dbprefix."tbshare_cates");
		$arrCates = array();
		foreach($tmp as $val) {
			$arrCates[$val['cateid']] = $val['catename'];
		}
		include template("admin/item");
		break;
	case 'edit':
		$tbshareid = $_GET['tbshareid'];
		$item = $db->once_fetch_assoc("select * from ".dbprefix."tbshare where tbshareid='$tbshareid'");
		
		$arrCates = $db->fetch_all_assoc("select cateid,catename from ".dbprefix."tbshare_cates");
		
		include template("admin/item_edit");
		break;
	//分类修改执行 
	case "edit_do":
		$tbshareid	 = $_POST['tbshareid'];
		$title		 = $_POST['title'];
		$cateid		 = intval($_POST['cateid']);
		$price		 = intval(floatval($_POST['price']) * 100);

				
		$db->query("update ".dbprefix."tbshare set `title`='$title',`cateid`='$cateid',`price`='$price' where tbshareid='$tbshareid'");
		
		header("Location: ".SITE_URL."index.php?app=tbshare&ac=admin&mg=item&ts=list");
		
		break;
	//分类删除
	case "del":
		
		$tbshareid = intval($_GET['tbshareid']);
		$tbshare = $db->once_fetch_assoc("select * FROM ".dbprefix."tbshare where tbshareid='$tbshareid'");
		if($tbshare) {
			$cateid = $tbshare['cateid'];
			//更新类别数目
			$db->query("UPDATE ".dbprefix."tbshare_cates SET num=num-1 WHERE cateid='$cateid'");
			//更新用户分享数
			$userid = $tbshare['userid'];
			$db->query("UPDATE ".dbprefix."tbshare_user SET sharecount=sharecount-1 WHERE userid='$userid'");

			//删除相关评论
			$db->query("DELETE FROM ".dbprefix."tbshare_comment WHERE tbshareid='$tbshareid'");
			//删除相关喜欢
			$db->query("DELETE FROM ".dbprefix."tbshare_favor WHERE tbshareid='$tbshareid'");
			
			$db->query("delete from ".dbprefix."tbshare where tbshareid='$tbshareid'");
			qiMsg("商品删除成功！");
		}
		break;
	
}


